package com.myjob.system.utils;

import com.shopgoods.common.core.web.domain.AjaxResult;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author: 作者
 * @create: 2024-08-22 14:16
 * @Description: 遇到异常, 给提示而不是异常终止-
 *
 * 底层 AOP原理
 */
@RestControllerAdvice
public class ResultExcptionHandler {
    /**
     * MethodArgumentNotValidException   方法参数不合法异常 [Validation对参数进行的验证]
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public AjaxResult methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException ex) {
        ex.printStackTrace();//打印异常栈信息
        String message = ex.getMessage();
        return AjaxResult.error().msg(message);
    }

    /**
     * ConstraintViolationException  处理路径参数不合法的异常
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public AjaxResult constraintViolationExceptionHandler(ConstraintViolationException ex) {
        ex.printStackTrace();
        String message = ex.getMessage();
        return AjaxResult.error().msg(message);
    }


}
